package com.warski.wonderfires.page.led;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;

import com.warski.wonderfires.net.JsonStep;
import com.warski.wonderfires.net.TCPConn;

import java.lang.reflect.Field;

public class LooperLayoutManager extends RecyclerView.LayoutManager {
    private static final String TAG = "LooperLayoutManager";
    private boolean looperEnable = true;
    private Handler mHandler;

    public LooperLayoutManager(Handler handler) {
        mHandler = handler;
    }

    public void setLooperEnable(boolean looperEnable) {
        this.looperEnable = looperEnable;
    }

    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public boolean canScrollHorizontally() {
        return false;
    }

    @Override
    public boolean canScrollVertically() {
        return true;
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        if (getItemCount() <= 0) {
            return;
        }
        //preLayout主要支持动画，直接跳过
        if (state.isPreLayout()) {
            return;
        }
        //将视图分离放入scrap缓存中，以准备重新对view进行排版
        detachAndScrapAttachedViews(recycler);

        int actualHeight = 0;
        for (int i = 0; i < getItemCount(); i++) {
            //初始化，将在屏幕内的view填充
            View itemView = recycler.getViewForPosition(i);
            addView(itemView);
            //测量itemView的宽高
            measureChildWithMargins(itemView, 0, 0);
            int width = getDecoratedMeasuredWidth(itemView);
            int height = getDecoratedMeasuredHeight(itemView);
            //根据itemView的宽高进行布局
            layoutDecorated(itemView, 0, actualHeight, width, actualHeight + height);
            actualHeight += height;
            //如果当前布局过的itemView的宽度总和大于RecyclerView的宽，则不再进行布局
            if (actualHeight > getHeight()) {
                break;
            }
        }
        int center = getChildCount()/2 - 1;
        for (int i = 0; i < getChildCount(); i++) {

            View view = getChildAt(i);
            if (view == null) {
                continue;
            }
            LinearLayout linearLayout = (LinearLayout) view;
            TextView textView = (TextView) linearLayout.getChildAt(0);
            textView.setGravity(Gravity.CENTER);
            if (i < center) {
                textView.setTextSize(18);
                textView.setTextColor(Color.WHITE);
            } else if (i == center) {
                textView.setTextSize(26);
                textView.setTextColor(centerColor);
            } else {
                textView.setTextSize(18);
                textView.setTextColor(Color.WHITE);
            }
        }
    }

    @Override
    public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        //1.上下滑动的时候，填充子view
        int travl = fill(dy, recycler, state);
        if (travl == 0) {
            return 0;
        }

        //2.滚动
        offsetChildrenVertical(travl * -1);

        //3.回收已经离开界面的
        recyclerHideView(dy, recycler, state);
        return travl;
    }

    /**
     * 上下滑动的时候，填充
     */
    private int fill(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        if (dy > 0) {
            //标注1.向上滚动
            View lastView = getChildAt(getChildCount() - 1);
            if (lastView == null) {
                return 0;
            }
            int lastPos = getPosition(lastView);
            //标注2.可见的最后一个itemView完全滑进来了，需要补充新的
            if (lastView.getBottom() < getHeight()) {
                View scrap = null;
                //标注3.判断可见的最后一个itemView的索引，
                // 如果是最后一个，则将下一个itemView设置为第一个，否则设置为当前索引的下一个
                if (lastPos == getItemCount() - 1) {
                    if (looperEnable) {
                        scrap = recycler.getViewForPosition(0);
                    } else {
                        dy = 0;
                    }
                } else {
                    scrap = recycler.getViewForPosition(lastPos + 1);
                }
                if (scrap == null) {
                    return dy;
                }
                //标注4.将新的itemViewadd进来并对其测量和布局
                addView(scrap);
                measureChildWithMargins(scrap, 0, 0);
                int width = getDecoratedMeasuredWidth(scrap);
                int height = getDecoratedMeasuredHeight(scrap);
                layoutDecorated(scrap, 0, lastView.getBottom(), width,
                        lastView.getBottom() + height);
                return dy;
            }
        } else {
            //向下滚动
            View firstView = getChildAt(0);
            if (firstView == null) {
                return 0;
            }
            int firstPos = getPosition(firstView);
            if (firstView.getTop() >= 0) {
                View scrap = null;
                if (firstPos == 0) {
                    if (looperEnable) {
                        scrap = recycler.getViewForPosition(getItemCount() - 1);
                    } else {
                        dy = 0;
                    }
                } else {
                    scrap = recycler.getViewForPosition(firstPos - 1);
                }
                if (scrap == null) {
                    return 0;
                }
                addView(scrap, 0);
                measureChildWithMargins(scrap, 0, 0);
                int width = getDecoratedMeasuredWidth(scrap);
                int height = getDecoratedMeasuredHeight(scrap);
                layoutDecorated(scrap, 0, firstView.getTop() - height,
                        width, firstView.getTop());
            }
        }

        return dy;
    }

    /**
     * 设定RecyclerView最大滑动速度
     */
    public void setMaxFlingVelocity(RecyclerView recycleview, int velocity) {
        try {
            Field field = recycleview.getClass().getDeclaredField("mMaxFlingVelocity");
            field.setAccessible(true);
            field.set(recycleview, velocity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 回收界面不可见的view
     */

    private int centerColor = Color.rgb(255,127,80);
    private int oldData = 5;
    private boolean ok = true;
    private Runnable mRunnable = () -> ok = true;
    private void recyclerHideView(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
        int center = getChildCount()/2 - 1;
        for (int i = 0; i < getChildCount(); i++) {

            View view = getChildAt(i);
            if (view == null) {
                continue;
            }

            if (dy > 0) {
                //向上滚动，移除一个上边不在内容里的view
                if (view.getBottom() < 0) {
                    removeAndRecycleView(view, recycler);
                    Log.d(TAG, "循环: 上滑移除 一个view  childCount=" + getChildCount());
                }
            } else {
                //向下滚动，移除一个下边不在内容里的view
                if (view.getTop() > getHeight()) {
                    removeAndRecycleView(view, recycler);
                    int count = getChildCount();
                    Log.d(TAG, "循环: 下滑移除 一个view  childCount=" + count);
                }
            }
            LinearLayout linearLayout = (LinearLayout) view;
            TextView textView = (TextView) linearLayout.getChildAt(0);
            textView.setGravity(Gravity.CENTER);
            if (i < center){
                textView.setTextSize(18);
                textView.setTextColor(Color.WHITE);
            }else if (i == center){
                textView.setTextSize(26);
                textView.setTextColor(centerColor);
                String[] strings = textView.getText().toString().split(" ");
                /*int data = Integer.parseInt(strings[0]);
                if (strings[1].contains("幕")) data += 41;
                else if (strings[1].contains("过渡")) data += 67;
                else if (strings[1].contains("流水")) data += 87;
                else if (strings[1].contains("流动")) data += 105;
                else if (strings[1].contains("拖尾")) data += 129;
                else if (strings[1].contains("跑动")){
                    if (strings[1].contains("正向")) data += 145;
                    else data += 176;
                }
                if (!(oldData == data)){
                    oldData = data;
                    Message message = new Message();
                    message.what = 1;
                    message.obj = String.valueOf(data);
                    mHandler.sendMessage(message);
                }*/
                String data = "model-1";
                if (strings[1].contains("幕")) data = "model-6";
                else if (strings[1].contains("过渡")) data = "model-4";
                else if (strings[1].contains("流水")) data = "model-3";
                else if (strings[1].contains("流动")) data = "model-4";
                else if (strings[1].contains("拖尾")) data = "model-5";
                else if (strings[1].contains("跑动")){
                    if (strings[1].contains("正向")) data = "model-6";
                    else data = "model-1";
                }
                if (ok){
                    ok = false;
                    Message message = new Message();
                    message.what = 1;
                    message.obj = data;
                    mHandler.sendMessage(message);
                    mHandler.postDelayed(mRunnable,500);
                }
            }else {
                textView.setTextSize(18);
                textView.setTextColor(Color.WHITE);
            }
        }
    }
}
